@using AyBorg.Web.Shared.Models.Agent

<MudTable Items="@Projects" Hover="true" Elevation="0">
    <HeaderContent>
        <MudTh Style="width:20px">Active</MudTh>
        <MudTh>Name</MudTh>
        <MudTh Style="width:20%">Change date</MudTh>
        <MudTh Style="width:20%">Creation date</MudTh>
        <MudTh Style="width: 200px"></MudTh>
    </HeaderContent>
    <RowTemplate>
        <MudTd DataLabel="Active">
            @if (@context.IsActive)
            {
                <MudIcon Icon="@Icons.Material.Filled.Check" />
            }
        </MudTd>
        <MudTd DataLabel="Name">@context.Name</MudTd>
        <MudTd DataLabel="Change date">@context.ChangeDate</MudTd>
        <MudTd DataLabel="Creation date">@context.CreationDate</MudTd>
        <MudTd DataLabel="Actions">
            @if (@context.IsActive)
            {
                <MudButton class="mb-4" Variant="Variant.Text" Color="Color.Primary" Disabled>Activate</MudButton>
            }
            else
            {
                <MudButton class="mb-4" Variant="Variant.Text" Color="Color.Primary"
                    @onclick="(args) => OnActivateClicked(args, context)">Activate</MudButton>
            }

            <MudMenu Icon="@Icons.Material.Filled.MoreVert" AnchorOrigin="Origin.BottomRight">
                @if (@context.State == SDK.Projects.ProjectState.Draft) {
                    <MudMenuItem Icon="@Icons.Material.Filled.SafetyCheck" IconColor="Color.Warning" @onclick="(args) => OnSaveAsReview(args, context)">
                            Create review
                    </MudMenuItem>
                }
                @if (@context.State == SDK.Projects.ProjectState.Review) {
                    <MudMenuItem Icon="@Icons.Material.Filled.Cancel" IconColor="Color.Error" @onclick="(args) => OnAbandonClicked(args, context)">
                            Abandon
                    </MudMenuItem>
                    <AuthorizeView Roles="Administrator, Reviewer" Context="other">
                        <Authorized>
                            <MudMenuItem Icon="@Icons.Material.Filled.GppGood" IconColor="Color.Success" @onclick="(args) => OnSaveAsReady(args, context)">
                                    Approve
                            </MudMenuItem>
                        </Authorized>
                    </AuthorizeView>
                }
                <MudDivider />
                <MudMenuItem Icon="@Icons.Material.Filled.DeleteForever" IconColor="Color.Error" @onclick="(args) => OnDeleteClicked(args, context)">
                    Delete
                </MudMenuItem>
            </MudMenu>
        </MudTd>
    </RowTemplate>
</MudTable>

@code {
    [Parameter]
    [EditorRequired]
    public IEnumerable<ProjectMeta> Projects { get; set; } = new List<ProjectMeta>();

    [Parameter]
    [EditorRequired]
    public EventCallback<ProjectMeta> OnProjectActivateClicked { get; set; }

    [Parameter]
    [EditorRequired]
    public EventCallback<ProjectMeta> OnProjectDeleteClicked { get; set; }

    [Parameter]
    public EventCallback<ProjectMeta> OnProjectSaveAsReviewClicked { get; set; }

    [Parameter]
    public EventCallback<ProjectMeta> OnProjectSaveAsReadyClicked { get; set; }

    [Parameter]
    public EventCallback<ProjectMeta> OnProjectAbandonClicked { get; set; }

    private async Task OnActivateClicked(EventArgs args, ProjectMeta projectDto)
    {
        await OnProjectActivateClicked.InvokeAsync(projectDto);
    }

    private async Task OnDeleteClicked(EventArgs args, ProjectMeta projectDto)
    {
        await OnProjectDeleteClicked.InvokeAsync(projectDto);
    }

    private async Task OnSaveAsReview(EventArgs args, ProjectMeta projectDto)
    {
        await OnProjectSaveAsReviewClicked.InvokeAsync(projectDto);
    }

    private async Task OnSaveAsReady(EventArgs args, ProjectMeta projectDto)
    {
        await OnProjectSaveAsReadyClicked.InvokeAsync(projectDto);
    }

    private async Task OnAbandonClicked(EventArgs args, ProjectMeta projectDto)
    {
        await OnProjectAbandonClicked.InvokeAsync(projectDto);
    }
}
